﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Hisun.Models;
using Hisun.Controllers;

using System.Data.Linq;
using System.Collections.Generic;

namespace Hisun.Controllers
{
    public class ChildCenterList
    {
        private IQueryable<GetCostCenterListResult> _CostCenterlist;
        public IQueryable<GetCostCenterListResult> CostCenterlist 
        { 
            get { return _CostCenterlist; } 
            set { }
        }
         HashSet<int> Centerlist = new HashSet<int>();
      //  string CenterIdArray = "";
        HisunDBDataContext db = new HisunDBDataContext();
       public  ChildCenterList()
        {
            _CostCenterlist = null;  

        }
       public ChildCenterList(int CenterID)
        {
            GetCostCenters( CenterID);
        }
        public int[] ChildCenterListItems(int CenterId)
        {
        //    var myquery = from a in db.TABC_JS_CostCenter select a;

        //    FillCenterList(CenterId, myquery);
            if (_CostCenterlist == null)
            {
                GetCostCenters(CenterId);
            }
            foreach (var CostCenter in _CostCenterlist)
            {
                Centerlist.Add(CostCenter.作业中心ID.Value);
            }
           
            if (Centerlist.Count == 0)
            {
                Centerlist.Add(0);
                return Centerlist.ToArray();
            }
            else
            {
                return Centerlist.ToArray();
            }
            
        }

        ////查询当前作业中心ID对应的子作业中心列表
        //private void FillCenterList(int CenterID, IEnumerable<TABC_JS_CostCenter> TotalTrees)
        //{
        //    var Mylist = (from m in TotalTrees where m.上级作业中心ID == CenterID select m).ToList();

        //    if (Mylist.Count() == 0)
        //        return;
        //    foreach (var MylistItem in Mylist)
        //    {
        //        Centerlist.Add(MylistItem.作业中心ID);
        //      //  CenterIdArray = CenterIdArray + "," + MylistItem.作业中心ID.ToString();
        //        FillCenterList(MylistItem.作业中心ID, TotalTrees);

        //    }
        //}
        /// <summary>
        /// 取下级作业中心列表
        /// </summary>
        /// <param name="CenterID"></param>
        /// <returns></returns>
        public void GetCostCenters(int CenterID)
        {
           var q =from a in db.GetCostCenterList(CenterID)
                  orderby a.上级作业中心ID,a.作业中心ID
                                   select a;
           this._CostCenterlist = q.AsQueryable(); 
        }

    }
}
